package com.example.zte.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

public class UserLoginActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    //定义字符串用来保存各个信息
    private String phone_str = "";
    private String paswd_str = "";
    //默认为选中"男性"
    private String sex_str = "男性";
    private String hobby_str = "1";
    private String city_str = "";
    //组件定义
    private EditText phone_edit;
    private EditText paswd_edit;

    private RadioGroup sex_group;
    private RadioButton nan_but;
    private RadioButton nv_but;

    private CheckBox play;
    private CheckBox read;
    private CheckBox music;

    private Spinner spinner;

    private Button register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);
        //组件初始化
        phone_edit = (EditText) findViewById(R.id.phone);
        paswd_edit = (EditText) findViewById(R.id.paswd);
        sex_group = (RadioGroup) findViewById(R.id.sex);
        //添加监听事件
        sex_group.setOnCheckedChangeListener(this);
        nan_but = (RadioButton) findViewById(R.id.nan);
        read = (CheckBox) findViewById(R.id.read_book);
        play = (CheckBox) findViewById(R.id.play_ball);
        music = (CheckBox) findViewById(R.id.music);
        register = (Button) findViewById(R.id.register);
        //添加监听事件
        register.setOnClickListener(this);
        spinner = (Spinner) findViewById(R.id.spinner);

        final String[] city = new String[]{"北京", "上海", "武汉", "南京", "南昌", "信阳"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, city);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                city_str = city[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.register:
                //获取手机号和密码
                phone_str = phone_edit.getText().toString();
                paswd_str = paswd_edit.getText().toString();
                //获取兴趣爱好即复选框的值
                hobby_str = "";   //清除上一次已经选中的选项
                if (read.isChecked()) {
                    hobby_str += read.getText().toString();
                }
                if (play.isChecked()) {
                    hobby_str += play.getText().toString();
                }
                if (music.isChecked()) {
                    hobby_str += music.getText().toString();
                }
                Intent intent = new Intent(this, SecondActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("phone", phone_str);
                bundle.putString("paswd", paswd_str);
                bundle.putString("sex", sex_str);
                bundle.putString("hobby", hobby_str);
                bundle.putString("city", city_str);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        sex_str = checkedId == R.id.nan ? "男性" : "女性";
    }
}